﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi.PubDS
{
    [CPPEI.WebApi.AuthGetAction("PubDS", "GetParameterList", "1.0")]
    public class GetParameterListAction : BaseAction<GetParameterListParams, GetParameterListResult>
    {
        protected override ActionResult<GetParameterListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetParameterListParams opParams)
        {
            SER.PubSourceService dtSerService = new SER.PubSourceService();
            ListResult<ENT.EPubParameter> serResult = dtSerService.GetParameters(opParams.DataSourceId);
            if(serResult.IsSuccess)
            {
                GetParameterListResult result = new GetParameterListResult();
                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    GetParameterListResult.Item item = new GetParameterListResult.Item();
                    item.Id = serResult.Data[index].Id;
                    item.Name = DefaultValue.GetValue(serResult.Data[index].Name, string.Empty);
                    item.Description = DefaultValue.GetValue(serResult.Data[index].Description, string.Empty);
                    item.Type = DefaultValue.GetValue(serResult.Data[index].Type, string.Empty);
                    item.Index = serResult.Data[index].Index;

                    result.Items.Add(item);
                }
                return new ActionResult<GetParameterListResult>(result);
            }
            else
            {
                return new ActionResult<GetParameterListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetParameterListParams
    {
        public Guid DataSourceId { get; set; }
    }

    public class GetParameterListResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }

            public string Name { get; set; }

            public string Description { get; set; }

            public string Type { get; set; }

            public int Index { get; set; }
        }
    }
}
